﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using SMTPAPI.Interfaces;
using SMTPAPI.InterfaceImplementation;
using SMTPAPI.Exceptions;
using System.Net;
using SMTPAPI.Enums;

namespace SMTPAPI.MainClass
{
    public class MailGenerator
    {
        private MailMessage _mailMessage;
        private IList<RecieversList> _recieverList;
        private IValidation _validation;
        private SmtpClient _smtpClient;
        private IMail _mail;

        public MailGenerator()
        {
            _validation = new Validation();
            _mail = new Mail();
            _recieverList = new List<RecieversList>();

        }
        public bool Generate(string userName, string password, string toList, string ccList, string bccList, IList<string> attachmentList, string messageBody, string messageSubject, bool isHtmlBody, string mesageHeaderPath, string smtpServerName, int port)
        {
            try
            {
                CreateMailBasicFormat(userName, messageSubject, messageBody, isHtmlBody, mesageHeaderPath);
                _recieverList.Add(new RecieversList { RecieverList = AddRecieverList(toList), RecieverType = RecieverTypes.TO });
                _recieverList.Add(new RecieversList { RecieverList = AddRecieverList(ccList), RecieverType = RecieverTypes.Cc });
                _recieverList.Add(new RecieversList { RecieverList = AddRecieverList(bccList), RecieverType = RecieverTypes.Bcc });
                AttachingAttachment(attachmentList);
                SetSMTPCredential(userName, password, smtpServerName, port);

                return (_mail.SendMail(_smtpClient, _mailMessage, _recieverList));

            }
            catch (InvalidEmailException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        private void CreateMailBasicFormat(string userName, string messageSubject, string messageBody, bool isHtmlBody, string mesageHeaderPath)
        {
            //Create mail message object and set Mail subject and Mail body.
            SenderInfo senderInfo = new SenderInfo();
            _mailMessage = senderInfo.CreateMailBasicFormat(userName, messageSubject, messageBody, isHtmlBody, mesageHeaderPath);

        }

        private IList<string> AddRecieverList(string recieverList)
        {
            try
            {
                IList<string> rList = new List<string>();
                string[] receiever = recieverList.Split(',');
                foreach (string rec in receiever)
                {
                    if (_validation.IsValidaEmail(rec))
                    {
                        rList.Add(rec);
                    }
                }

                return rList;
            }
            catch (InvalidEmailException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void AttachingAttachment(IList<string> attachmentList)
        {
            AttachAttachment attachAttachment = new AttachAttachment();
            attachAttachment.AttachedAttachment(attachmentList, _mailMessage);
        }

        private void SetSMTPCredential(string userName, string password, string smtpServerName, int port)
        {
            SMTPSeverSettings smtpSeverSettings = new SMTPSeverSettings();
            _smtpClient = smtpSeverSettings.ConfigureSMTPServerSetting(smtpServerName, port);
            _smtpClient.Credentials = new NetworkCredential(userName, password);
        }
    }
}
